<?php
/**
 * @title 2865. 美丽塔 I - 单调栈 + 前缀和
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $maxHeights
     * @return Integer
     */
    function maximumSumOfHeights($maxHeights) {
        /**
         * @since 2024-01-24 前缀和，leftSumArray[i]表示，下标[0, i]的递增和
         */
        $leftSumArray = $rightSumArray = [];
        $leftSum = $rightSum = 0;

        /**
         * @since 2024-01-24 数组栈
         */
        $leftStack = $rightStack = [];
        $leftP = $rightP = -1;

        /**
         * @since 2024-01-24 maxHeights的元素个数
         */
        $maxHeightsLength = count($maxHeights);

        /**
         * @since 2024-01-24 遍历数组maxHeights
         */
        for($left=0, $right=$maxHeightsLength-1; $left < $maxHeightsLength; $left++, $right--){
            /**
             * @since 2024-01-24 maxHeights[left]比左边的数字都要大于等于，弹出栈内大于maxHeights[left]的元素
             */
            $leftCount = 1;
            while($leftP >= 0 AND $maxHeights[$left] < $leftStack[$leftP][0]){
                $leftSum = $leftSum - $leftStack[$leftP][0]*$leftStack[$leftP][1];

                /**
                 * @since 2024-01-24 相同数量的maxHeight[left]顶替
                 */
                $leftCount += $leftStack[$leftP][1];
                $leftP--;
            }
            $leftStack[++$leftP] = [$maxHeights[$left], $leftCount];

            /**
             * @since 2024-01-24 更新leftSum
             */
            $leftSum += $maxHeights[$left] * $leftCount;
            $leftSumArray[$left] = $leftSum;

            /**
             * @since 2024-01-24 从右到左
             */
            $rightCount = 1;
            while($rightP >= 0 AND $maxHeights[$right] < $rightStack[$rightP][0]){
                $rightSum = $rightSum - $rightStack[$rightP][0]*$rightStack[$rightP][1];
                $rightCount += $rightStack[$rightP][1];
                $rightP--;
            }
            $rightStack[++$rightP] = [$maxHeights[$right], $rightCount];
            $rightSum += $maxHeights[$right] * $rightCount;
            $rightSumArray[$right] = $rightSum;
        }
        // return [$leftSumArray, $rightSumArray];

        /**
         * @since 2024-01-24 高度和最大值
         */
        $heightSumMax = 0;
        for($i=0; $i<$maxHeightsLength; $i++){
            $heightSumMax = max($heightSumMax, $leftSumArray[$i]+$rightSumArray[$i]-$maxHeights[$i]);
        }

        /**
         * @return
         */
        return $heightSumMax;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.24.php
 */
$datas = [
    [[5,3,4,1,1]], // 13
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->maximumSumOfHeights(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
